
    # public String ConvertRGBToHex(int r, int g, int b) {
    #     String hex = "";
    #     if (r >= 0 && r < 256 && g >= 0 && g < 256 && b >= 0 && b < 256) {
    #         int x, y, z;
    #         x = r % 16;
    #         r = (r - x) / 16;
    #         y = g % 16;
    #         g = (g - y) / 16;
    #         z = b % 16;
    #         b = (b - z) / 16;
    #         hex = "#" + s.substring(r, r + 1) + s.substring(x, x + 1) + s.substring(g, g + 1) + s.substring(y, y + 1) + s.substring(b, b + 1) + s.substring(z, z + 1);
    #     }
    #     return hex;
    # }

def clamp(v, min, max):
    if v < min:
        v = min
    if v > max:
        v = max
    return v

def rgb_convert_hexstring(r, g, b):
    hs = ""
    s = "0123456789abcdef"
    r = clamp(r, 0, 255)
    g = clamp(g, 0, 255)
    b = clamp(b, 0, 255)
    x = r % 16
    r = int((r - x) / 16)
    y = g % 16
    g = int((g - y) / 16)
    z = b % 16
    b = int((b - z) / 16)
    hs = "#" + s[r] + s[x] + s[g] + s[y] + s[b] + s[z]
    return hs

def rgb_convert_hexstring(rgb_int):
    r = rgb_int >> 16
    g = (rgb_int & 0x0000ff00) >> 8
    b = rgb_int & 0x000000ff

    hs = ""
    s = "0123456789abcdef"
    r = clamp(r, 0, 255)
    g = clamp(g, 0, 255)
    b = clamp(b, 0, 255)
    x = r % 16
    r = int((r - x) / 16)
    y = g % 16
    g = int((g - y) / 16)
    z = b % 16
    b = int((b - z) / 16)
    hs = "#" + s[r] + s[x] + s[g] + s[y] + s[b] + s[z]
    return hs

def hexstring_to_rgb(hexstring):
    rgb = {}
    r = int(hexstring[1:3], 16)
    g = int(hexstring[3:5], 16)
    b = int(hexstring[5:7], 16)
    rgb["r"] = r
    rgb["g"] = g
    rgb["b"] = b
    return rgb

# print(rgb_convert_hexstring(255, 180, 10))

def test():
    h = rgb_convert_hexstring(255, 180, 10)
    print(h)

    rgb = hexstring_to_rgb(h)
    print(rgb)

